const app = getApp();

let rewardedVideoAd = null;
let interAd = null;

Page({
    data: {
        num1: '',
        num2: '',
        num3: '',
        num4: '',
        solutions: []
    },
    onLoad: function (options) {
      this.LoadAd();
    },
    handleInput1: function (e) {
        this.setData({
            num1: e.detail.value
        });
    },
    handleInput2: function (e) {
        this.setData({
            num2: e.detail.value
        });
    },
    handleInput3: function (e) {
        this.setData({
            num3: e.detail.value
        });
    },
    handleInput4: function (e) {
        this.setData({
            num4: e.detail.value
        });
    }
    ,
  showTip: function () {
    if (rewardedVideoAd) {
      console.log("show ad");
      rewardedVideoAd.show();
    } else
      this.showSolutions();
  },
    // 显示提示信息
    showSolutions: function () {
        app.playSound('enniu');
        const numbers = [this.data.num1, this.data.num2, this.data.num3, this.data.num4];
        //console.log('提示', numbers);
        const solutions = this.find24(numbers);
        //console.log('提示2', solutions);
        if(solutions.length == 0)
        {
          solutions.push('无解，请重新输入数值');
        }
        this.setData({
            solutions: solutions
        });
    },
    // 求解 24 点问题
    find24: function (nums) {
        if (nums.length !== 4) {
            return ["请输入4个数字"];
        }

        const allSolutions = [];

        // 递归函数，尝试所有可能的运算组合
        function dfs(nums, path) {
            if (nums.length === 1) {
                if (Math.abs(nums[0] - 24) < 1e-6) {
                    allSolutions.push(path[0]);
                    return;
                }
                return;
            }

            for (let i = 0; i < nums.length; i++) {
                for (let j = 0; j < nums.length; j++) {
                    if (i === j) continue;

                    const nextNums = [];
                    const nextPath = [];
                    for (let k = 0; k < nums.length; k++) {
                        if (k !== i && k !== j) {
                            nextNums.push(nums[k]);
                            nextPath.push(path[k]);
                        }
                    }

                    // 尝试加法
                    const addResult = nums[i] + nums[j];
                    const addPath = `(${path[i]} + ${path[j]})`;
                    nextNums.push(addResult);
                    nextPath.push(addPath);
                    dfs(nextNums, nextPath);
                    nextNums.pop();
                    nextPath.pop();

                    // 尝试减法
                    const subResult = nums[i] - nums[j];
                    const subPath = `(${path[i]} - ${path[j]})`;
                    nextNums.push(subResult);
                    nextPath.push(subPath);
                    dfs(nextNums, nextPath);
                    nextNums.pop();
                    nextPath.pop();

                    // 尝试乘法
                    const mulResult = nums[i] * nums[j];
                    const mulPath = `(${path[i]} * ${path[j]})`;
                    nextNums.push(mulResult);
                    nextPath.push(mulPath);
                    dfs(nextNums, nextPath);
                    nextNums.pop();
                    nextPath.pop();

                    // 尝试除法
                    if (nums[j] !== 0) {
                        const divResult = nums[i] / nums[j];
                        const divPath = `(${path[i]} / ${path[j]})`;
                        nextNums.push(divResult);
                        nextPath.push(divPath);
                        dfs(nextNums, nextPath);
                        nextNums.pop();
                        nextPath.pop();
                    }
                }
            }
        }

        const path = nums.map(num => num.toString());
        dfs(nums, path);

        return allSolutions;
    },
    LoadAd: function(){
      if (wx.createInterstitialAd && !interAd) {
        interAd = wx.createInterstitialAd({
          adUnitId: 'adunit-c4fd603746917cbb'
        })
        interAd.onLoad(() => {
          console.log('interstitialAd onLoad event emit')
        })
        interAd.onError((err) => {
          console.log('interstitialAd onError event emit', err)
        })
        interAd.onClose((res) => {
          console.log('interstitialAd onClose event emit', res)
        })
      }
  
      if (wx.createRewardedVideoAd && !rewardedVideoAd) {
        rewardedVideoAd = wx.createRewardedVideoAd({
          adUnitId: 'adunit-bb0a6e83898021de'
        })
        rewardedVideoAd.onLoad(() => {
          console.log('rewardedVideoAd onLoad event emit')
        })
        rewardedVideoAd.onError((err) => {
          console.log('rewardedVideoAd onError event emit', err)
          this.showSolutions();
        })
        rewardedVideoAd.onClose((status) => {
          if (status && status.isEnded || status === undefined) {
            console.log('rewardedVideoAd 正常播放结束，下发奖励', status)
            // 正常播放结束，下发奖励
            this.showSolutions();
          } else {
            console.log('rewardedVideoAd 播放中途退出，进行提示', status)
            // 播放中途退出，进行提示
            wx.showToast({
              title: '请播放完毕',
            })
          }
        })
      }
    },
  LoadAd: function(){
    if (wx.createInterstitialAd && !interAd) {
      interAd = wx.createInterstitialAd({
        adUnitId: 'adunit-bb0a6e83898021de'
      })
      interAd.onLoad(() => {
        console.log('interstitialAd onLoad event emit')
      })
      interAd.onError((err) => {
        console.log('interstitialAd onError event emit', err)
      })
      interAd.onClose((res) => {
        console.log('interstitialAd onClose event emit', res)
      })
    }

    if (wx.createRewardedVideoAd && !rewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-183e27fa0863ede5'
      })
      rewardedVideoAd.onLoad(() => {
        console.log('rewardedVideoAd onLoad event emit')
      })
      rewardedVideoAd.onError((err) => {
        console.log('rewardedVideoAd onError event emit', err)
        this.showSolutions();
      })
      rewardedVideoAd.onClose((status) => {
        if (status && status.isEnded || status === undefined) {
          console.log('rewardedVideoAd 正常播放结束，下发奖励', status)
          // 正常播放结束，下发奖励
          this.showSolutions();
        } else {
          console.log('rewardedVideoAd 播放中途退出，进行提示', status)
          // 播放中途退出，进行提示
          wx.showToast({
            title: '请播放完毕',
          })
        }
      })
    }
  }

});